<?php
class DiemchuansController extends AppController {

	var $name = 'Diemchuans';
	var $helpers=array('Ctable');
	
	function index() {
		$this->Diemchuan->recursive = 0;
		$this->set('diemchuans', $this->paginate());
	}

	function chonsanpham($kehoach_id)
	{
		$hanghoas = $this->Diemchuan->Hanghoa->find('list');
		$this->set(compact('hanghoas','kehoach_id'));
	}
	
	function themdiemchuan($kehoach_id)
	{
		$dalap=$this->Diemchuan->find('list',array('conditions'=>array('kehoach_id'=>$kehoach_id)));
		if(count($dalap)!=0)
			$this->redirect(array('controller'=>'Diemchuans','action'=>'hiendiemchuan',$kehoach_id));
		for($i=0;$i<$this->data['Diemchuan']['countcheck'];$i++)
		{
			if ($this->data['Diemchuan']['checkbox'.$i]!=0)
				$hanghoas[$this->data['Diemchuan']['checkbox'.$i]]=$this->data['Diemchuan'][$this->data['Diemchuan']['checkbox'.$i]];
		}
		$thoigian = $this->Diemchuan->Kehoach->find('list',array(
													'recursive'=>0,'contain'=>array('Thoigian'),
													'conditions'=>array('Kehoach.id'=>$kehoach_id),
													'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt')));
		$diemchuan["diemchua"]="Điểm Chuẩn";
		$this->set(compact('hanghoas','diemchuan','kehoach_id','thoigian'));
	}
	
	function suadiemchuan($kehoach_id)
	{
		
		$thoigian = $this->Diemchuan->Kehoach->find('list',array(
																'recursive'=>0,'contain'=>array('Thoigian'),
																'conditions'=>array('Kehoach.id'=>$kehoach_id),
																'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt')));
		$hanghoas=$this->Diemchuan->find('list',array(
								'recursive'=>0,'contain'=>array('Hanghoa'),
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('Hanghoa.id','Hanghoa.tenhh')));
		$diems =$this->Diemchuan->find('list',array(
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('hanghoa_id','diem')));
		$diemchuanid =$this->Diemchuan->find('list',array(
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('hanghoa_id','id')));
		$ids = array_keys($diems);
		$hh_ids = array_keys($hanghoas);
		$dong=0;
		$cot['diemchuan']="Điểm Chuẩn";
		foreach ($hanghoas as $key => $value):
		
			$data[$dong][0]=$diems[$key];
			$data_id[$dong][0]=$diemchuanid[$key];
			$dong++;
		endforeach;
		$this->set(compact('data','data_id','hanghoas','cot','kehoach_id','thoigian'));
	}
	
	function hiendiemchuan($kehoach_id=1)
	{
		$thoigian = $this->Diemchuan->Kehoach->find('list',array(
																'recursive'=>0,'contain'=>array('Thoigian'),
																'conditions'=>array('Kehoach.id'=>$kehoach_id),
																'fields'=>array('Thoigian.ngaybd','Thoigian.ngaykt')));
		$hanghoas=$this->Diemchuan->find('list',array(
								'recursive'=>0,'contain'=>array('Hanghoa'),
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('Hanghoa.id','Hanghoa.tenhh')));
		$diems =$this->Diemchuan->find('list',array(
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('hanghoa_id','diem')));
		$diemchuanid =$this->Diemchuan->find('list',array(
								'conditions'=>array('kehoach_id'=>$kehoach_id),
								'fields'=>array('hanghoa_id','id')));
		$ids = array_keys($diems);
		$hh_ids = array_keys($hanghoas);
		$dong=0;
		$cot['diemchuan']="Điểm Chuẩn";
		foreach ($hanghoas as $key => $value):
		
			$data[$dong][0]=$diems[$key];
			$dong++;
		endforeach;
		$this->set(compact('data','hanghoas','cot','kehoach_id','thoigian'));
	}
	
	function luudiemchuan($kehoach_id)
	{
		$rownum = $this->data['Diemchuan']['rownum'];
		$i=0;
             for($j=0;$j<$rownum;$j++)
             {
					if(isset($this->data['Diemchuan']['id'][$j][0]))
						$record['Diemchuan']['id']=$this->data['Diemchuan']['id'][$j][0]; 
             		$record['Diemchuan']['kehoach_id']=$kehoach_id;
                    $record['Diemchuan']['hanghoa_id']=$this->data['Diemchuan']['row'][$j];
                    if ($this->data['Diemchuan'][$j][$i]!=NULL)
                    	$record['Diemchuan']['diem']=$this->data['Diemchuan'][$j][0];
                    else
                    	$record['Diemchuan']['diem']=0;
                    if(!$this->Diemchuan->saveAll($record))
                    {
                         $this->Session->setFlash(__('Lưu Thất Bại', true));                     
             		}
    		}
    		$this->Session->setFlash(__('Đã Lập Điểm Chuẩn Cho trung Tâm', true));
			$this->redirect(array('controller'=>'Chitieudcns','action' => 'nhapsoluong',$kehoach_id));
	}

	
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid diemchuan', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('diemchuan', $this->Diemchuan->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Diemchuan->create();
			if ($this->Diemchuan->save($this->data)) {
				$this->Session->setFlash(__('The diemchuan has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The diemchuan could not be saved. Please, try again.', true));
			}
		}
		$hanghoas = $this->Diemchuan->Hanghoa->find('list');
		$kehoaches = $this->Diemchuan->Kehoach->find('list');
		$donvis = $this->Diemchuan->Donvi->find('list');
		$this->set(compact('hanghoas', 'kehoaches', 'donvis'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid diemchuan', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Diemchuan->save($this->data)) {
				$this->Session->setFlash(__('The diemchuan has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The diemchuan could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Diemchuan->read(null, $id);
		}
		$hanghoas = $this->Diemchuan->Hanghoa->find('list');
		$kehoaches = $this->Diemchuan->Kehoach->find('list');
		$donvis = $this->Diemchuan->Donvi->find('list');
		$this->set(compact('hanghoas', 'kehoaches', 'donvis'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for diemchuan', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Diemchuan->delete($id)) {
			$this->Session->setFlash(__('Diemchuan deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Diemchuan was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
